
//管理服务
define(["core","manage.request","angular.util"],function(core,manageRequest,util)
{
	var manageService={};
	
	manageService.menu=
	{
		//返回菜单
		findMenu:function(callback)
		{
			manageRequest.menu.findMenu(function(data)
			{
				if(core.util.isBlank(data))
					data=new Array();
					
				var root=[];
				var main=null;
				for(var i=0,len=data.length;i<len;i++)
				{
					var temp=data[i];
					if(temp.parent==0)
					{
						temp.children=[];
						root.push(temp);
					}
				}
				
				for(var i=0,len=root.length;i<len;i++)
				{
					for(var j=0;j<data.length;j++)
					{
						var temp=data[j];
						if(temp.parent==root[i].id)
							root[i].children.push(temp);
							
						if(temp.main)
							main=temp;
					}
				}
				
				data={data:root,main:main};
				callback(data);
			});
		}
	};
	
	manageService.config=
	{
		//返回配置
		find:function(callback)
		{
			manageRequest.config.find(util.dataCallback(callback));
		},
		//更新
		update:function(config,callback)
		{
			manageRequest.config.update(config.id,config.name,config.value,util.resultCallback(callback));
		}
	};
	
	manageService.illegalChar=
	{
		//返回
		find:function(callback)
		{
			manageRequest.illegalChar.find(util.dataCallback(callback));
		},
		//创建
		create:function(illegalChar,callback)
		{
			manageRequest.illegalChar.create(illegalChar.name,illegalChar.replace,illegalChar.source,util.resultCallback(callback));
		},
		//更新
		update:function(illegalChar,callback)
		{
			manageRequest.illegalChar.update(illegalChar.id,illegalChar.name,illegalChar.replace,illegalChar.source,util.resultCallback(callback));
		},
		//删除
		remove:function(id,callback)
		{
			manageRequest.illegalChar.remove(id,util.resultCallback(callback));
		}
	};
	
	manageService.openApi=
	{
		//返回
		find:function(callback)
		{
			manageRequest.openApi.find(function(data)
			{
				if(core.util.isBlank(data))
					data=new Array();
					
				var root=[];
				var error=[];
				for(var i=0,len=data.length;i<len;i++)
				{
					var temp=data[i];
					if(temp.parent==0)
					{
						temp.children=[];
						root.push(temp);
					}
				}
				
				for(var i=0;i<data.length;i++)
				{
					var temp=data[i];
					if(temp.parent!=0)
					{
						for(var j=0;j<root.length;j++)
						{
							if(temp.parent==root[j].id)
							{
								root[j].children.push(temp);
								break;
							}
							
							if(j==root.length-1)
								error.push(temp);
						}
						
						if(root.length==0)
							error.push(temp);
					}
				}
				data={root:root,error:error};
				callback(data);
			});
		},
		//创建
		create:function(openApi,callback)
		{
			manageRequest.openApi.create(openApi.name,openApi.url,openApi.format,openApi.request_type,openApi.parameter,
				openApi.explain,openApi.result,openApi.parent,openApi.index,util.resultCallback(callback));
		},
		//更新
		update:function(openApi,callback)
		{
			manageRequest.openApi.update(openApi.id,openApi.name,openApi.url,openApi.format,openApi.request_type,openApi.parameter,
				openApi.explain,openApi.result,openApi.parent,openApi.index,util.resultCallback(callback));
		},
		//删除		
		remove:function(id,callback)
		{
			manageRequest.openApi.remove(id,util.resultCallback(callback));
		}
	};
	
	manageService.module=
	{
		//获得模块
		findType:function(callback)
		{
			manageRequest.module.findType(util.dataCallback(callback));
		},
		//根据类型返回
		findByType:function(type,callback)
		{
			manageRequest.module.findByType(type,util.dataCallback(callback));
		}
	};
	
	manageService.user=
	{
		//搜索
		search:function(account,username,pn,callback)
		{
			manageRequest.user.search(account,username,pn,util.pageCallback(callback));
		},
		//更新角色
		updateRole:function(id,role,callback)
		{
			manageRequest.user.updateRole(id,role,util.resultCallback(callback));
		},
		//踢下线
		kick:function(id,callback)
		{
			manageRequest.user.kick(id,util.resultCallback(callback));
		},
		role:
		{
			//返回用户权限类型
			findType:function(callback)
			{
				manageRequest.user.role.findType(util.dataCallback(callback));
			},
			//更新类型
			update:function(roleType,callback)
			{
				manageRequest.user.role.update(roleType.id,roleType.name,util.resultCallback(callback));
			},
			//删除类型
			remove:function(id,target,callback)
			{
				manageRequest.user.role.remove(id,target,util.resultCallback(callback));
			},
			//根据角色和类型返回
			findByRoleAndType:function(role,type,callback)
			{
				manageRequest.user.role.findByRoleAndType(role,type,util.dataCallback(callback));
			},
			//更新角色模块
			updateModuleByRole:function(role,type,modules,callback)
			{
				manageRequest.user.role.updateModuleByRole(role,type,modules,util.resultCallback(callback));
			},
			//设置默认
			setDefault:function(role,callback)
			{
				manageRequest.user.role.setDefault(role,util.resultCallback(callback));
			},
			//创建
			create:function(role,callback)
			{
				manageRequest.user.role.create(role.name,util.resultCallback(callback));
			}
		},
		bar:
		{
			//返回
			find:function(callback)
			{
				manageRequest.user.bar.find(function(data)
				{
					if(core.util.isBlank(data))
						data=new Array();
						
					var root=[];
					var error=[];
					for(var i=0,len=data.length;i<len;i++)
					{
						var temp=data[i];
						if(temp.parent==0)
						{
							temp.children=[];
							root.push(temp);
						}
					}
					
					for(var i=0;i<data.length;i++)
					{
						var temp=data[i];
						if(temp.parent!=0)
						{
							for(var j=0;j<root.length;j++)
							{
								if(temp.parent==root[j].id)
								{
									root[j].children.push(temp);
									break;
								}
								
								if(j==root.length-1)
									error.push(temp);
							}
							
							if(root.length==0)
								error.push(temp);
						}
					}
					data={all:data,root:root,error:error};
					callback(data);
				});
			},
			//创建
			create:function(bar,callback)
			{
				manageRequest.user.bar.create(bar.icon,bar.text,bar.url,bar.click,bar.parent,bar.index,util.resultCallback(callback));
			},
			//更新
			update:function(bar,callback)
			{
				manageRequest.user.bar.update(bar.id,bar.icon,bar.text,bar.url,bar.click,bar.parent,bar.index,util.resultCallback(callback));
			},
			//删除
			remove:function(id,callback)
			{
				manageRequest.user.bar.remove(id,util.resultCallback(callback));
			},
			role:
			{
				//根据令牌返回
				findByToken:function(callback)
				{
					manageRequest.user.bar.role.findByToken(function(data)
					{
						if(core.util.isBlank(data))
							data=new Array();
							
						var root=[];
						for(var i=0,len=data.length;i<len;i++)
						{
							var temp=data[i];
							if(temp.parent==0)
							{
								temp.children=[];
								root.push(temp);
							}
						}
						
						for(var i=0,len=root.length;i<len;i++)
						{
							for(var j=0;j<data.length;j++)
							{
								var temp=data[j];
								if(temp.parent==root[i].id)
									root[i].children.push(temp);
							}
						}
						
						data=root;
						callback(data);
					});
				},
				//根据角色返回
				findByRole:function(role,callback)
				{
					manageRequest.user.bar.role.findByRole(role,util.dataCallback(callback));
				},
				//更新
				update:function(role,barRoles,callback)
				{
					manageRequest.user.bar.role.update(role,barRoles,util.resultCallback(callback));
				}
			}
		},
		menu:
		{
			//返回
			find:function(callback)
			{
				manageRequest.user.menu.find(function(data)
				{
					if(core.util.isBlank(data))
						data=new Array();
					
					var root=[];
					var error=[];
					for(var i=0,len=data.length;i<len;i++)
					{
						var temp=data[i];
						if(temp.parent==0)
						{
							temp.children=[];
							root.push(temp);
						}
					}
					
					for(var i=0;i<data.length;i++)
					{
						var temp=data[i];
						if(temp.parent!=0)
						{
							for(var j=0;j<root.length;j++)
							{
								if(temp.parent==root[j].id)
								{
									root[j].children.push(temp);
									break;
								}
								
								if(j==root.length-1)
									error.push(temp);
							}
							
							if(root.length==0)
								error.push(temp);
						}
					}
					data={all:data,root:root,error:error};
					callback(data);
				});
			},
			//创建
			create:function(menu,callback)
			{
				manageRequest.user.menu.create(menu.icon,menu.text,menu.url,menu.parent,menu.main,menu.index,util.resultCallback(callback));
			},
			//更新
			update:function(menu,callback)
			{
				manageRequest.user.menu.update(menu.id,menu.icon,menu.text,menu.url,menu.parent,menu.main,menu.index,util.resultCallback(callback));
			},
			//删除
			remove:function(id,callback)
			{
				manageRequest.user.menu.remove(id,util.resultCallback(callback));
			},
			role:
			{
				//根据令牌返回
				findByToken:function(callback)
				{
					manageRequest.user.menu.role.findByToken(function(data)
					{
						if(core.util.isBlank(data))
							data=new Array();
						
						var root=[];
						for(var i=0,len=data.length;i<len;i++)
						{
							var temp=data[i];
							if(temp.parent==0)
							{
								temp.children=[];
								root.push(temp);
							}
						}
						
						for(var i=0,len=root.length;i<len;i++)
						{
							for(var j=0;j<data.length;j++)
							{
								var temp=data[j];
								if(temp.parent==root[i].id)
									root[i].children.push(temp);
							}
						}
						
						data=root;
						callback(data);
					});
				},
				//根据角色返回
				findByRole:function(role,callback)
				{
					manageRequest.user.menu.role.findByRole(role,util.dataCallback(callback));
				},
				//更新
				update:function(role,menuRoles,callback)
				{
					manageRequest.user.menu.role.update(role,menuRoles,util.resultCallback(callback));
				}
			}
		},
		active:
		{
			type:
			{	
				//返回动态类型
				find:function(callback)
				{
					manageRequest.user.active.type.find(util.dataCallback(callback));
				},
				//更新
				update:function(active,callback)
				{
					manageRequest.user.active.type.update(active.id,active.name,active.message,active.url,util.resultCallback(callback));
				}
			}
		},
		mail:
		{
			type:
			{
				//返回
				find:function(callback)
				{
					manageRequest.user.mail.type.find(util.dataCallback(callback));
				},
				//更新
				update:function(mail,callback)
				{
					manageRequest.user.mail.type.update(mail.id,mail.name,mail.text,util.resultCallback(callback));
				}
			},
			system:
			{
				//返回
				find:function(pn,callback)
				{
					manageRequest.user.mail.system.find(pn,util.pageCallback(callback));
				},
				//创建
				create:function(mail,callback)
				{
					manageRequest.user.mail.system.create(mail.title,mail.content,mail.role,util.resultCallback(callback));
				}
			}
		}
	};
	
	manageService.software=
	{
		//返回正在审核的
		findCheck:function(pn,callback)
		{
			manageRequest.software.findCheck(pn,util.pageCallback(callback));
		},
		//审核通过
		checkPass:function(id,callback)
		{
			manageRequest.software.checkPass(id,util.resultCallback(callback));
		},
		//审核失败
		checkFailure:function(id,callback)
		{
			manageRequest.software.checkFailure(id,util.resultCallback(callback));
		},
		//更新
		update:function(software,callback)
		{
			manageRequest.software.update(software.id,software.name,software.title,software.icon,software.home_page,software.doc_url,software.download_url,software.introduce,util.resultCallback(callback));
		}
	};
	
	manageService.plugin=
	{
		//返回插件
		find:function(callback)
		{
			manageRequest.plugin.find(util.dataCallback(callback));
		},
		//设置激活
		setEnabled:function(id,enabled,callback)
		{
			manageRequest.plugin.setEnabled(id,enabled,util.resultCallback(callback));
		},
		//移除插件
		remove:function(id,callback)
		{
			manageRequest.plugin.remove(id,util.resultCallback(callback));
		}
	};
	
	manageService.discuss=
	{
		//返回已删除
		findDelete:function(pn,callback)
		{
			manageRequest.discuss.findDelete(pn,util.pageCallback(callback));
		},
		//恢复已删除
		recoverDelete:function(id,callback)
		{
			manageRequest.discuss.recoverDelete(id,util.resultCallback(callback));
		},
		//板块
		forum:
		{
			//返回
			find:function(callback)
			{
				manageRequest.discuss.forum.find(util.dataCallback(callback))
			},
			//创建
			create:function(forum,callback)
			{
				manageRequest.discuss.forum.create(forum.name,forum.check,forum.index_type,forum.index,util.resultCallback(callback));
			},
			//更新
			update:function(forum,callback)
			{
				manageRequest.discuss.forum.update(forum.id,forum.name,forum.check,forum.index_type,forum.index,util.resultCallback(callback));
			},
			//首页
			index:function(callback)
			{
				manageRequest.discuss.forum.index(util.dataCallback(callback));
			}
		},
		order:
		{
			//返回
			find:function(callback)
			{
				manageRequest.discuss.order.find(util.dataCallback(callback));
			},
			//创建
			create:function(order,callback)
			{
				manageRequest.discuss.order.create(order.name,order.color,order.font,order.index,util.resultCallback(callback));
			},
			//更新
			update:function(order,callback)
			{
				manageRequest.discuss.order.update(order.id,order.name,order.color,order.font,order.index,util.resultCallback(callback));
			},
			//设置默认
			setDefault:function(id,callback)
			{
				manageRequest.discuss.order.setDefault(id,util.resultCallback(callback));
			}
		},
		//返回审核
		findCheck:function(pn,callback)
		{
			manageRequest.discuss.findCheck(pn,util.pageCallback(callback));
		},
		//审核通过
		checkPass:function(id,callback)
		{
			manageRequest.discuss.checkPass(id,util.resultCallback(callback));
		},
		//审核不通过
		checkFailure:function(id,callback)
		{
			manageRequest.discuss.checkFailure(id,util.resultCallback(callback));
		},
		//删除
		remove:function(id,callback)
		{
			manageRequest.discuss.remove(id,util.resultCallback(callback));
		},
		report:
		{
			//返回
			find:function(pn,callback)
			{
				manageRequest.discuss.report.find(pn,util.pageCallback(callback));
			},
			//根据ID返回
			findById:function(id,callback)
			{
				manageRequest.discuss.report.findById(id,callback);
			}
		}
	};
	
	manageService.upload=
	{
		config:
		{
			//返回
			find:function(callback)
			{
				manageRequest.upload.config.find(util.dataCallback(callback));
			},
			//更新
			update:function(config,callback)
			{
				manageRequest.upload.config.update(config.id,config.name,config.path,config.size,config.type,util.resultCallback(callback));
			}
		},
		record:
		{
			//返回
			find:function(pn,callback)
			{
				manageRequest.upload.record.find(pn,util.dataCallback(callback));
			}
		}
	};
	
	manageService.friendlink=
	{
		//返回
		find:function(callback)
		{
			manageRequest.friendlink.find(util.dataCallback(callback));
		},
		//创建
		create:function(friendlink,callback)
		{
			manageRequest.friendlink.create(friendlink.name,friendlink.text,friendlink.url,friendlink.explain,friendlink.color,friendlink.font,friendlink.index,util.resultCallback(callback));
		},
		//更新
		update:function(friendlink,callback)
		{
			manageRequest.friendlink.update(friendlink.id,friendlink.name,friendlink.text,friendlink.url,friendlink.explain,friendlink.color,friendlink.font,friendlink.index,util.resultCallback(callback));
		},
		//删除
		remove:function(id,callback)
		{
			manageRequest.friendlink.remove(id,util.resultCallback(callback));
		}
	};
	
	manageService.dongtan=
	{
		//返回已删除
		findDelete:function(pn,callback)
		{
			manageRequest.dongtan.findDelete(pn,util.pageCallback(callback));
		},
		//恢复已删除
		recoverDelete:function(id,callback)
		{
			manageRequest.dongtan.recoverDelete(id,util.resultCallback(callback));
		}
	};
	
	manageService.blog=
	{
		order:
		{
			//返回排序
			find:function(callback)
			{
				manageRequest.blog.order.find(util.dataCallback(callback));
			},
			//创建
			create:function(order,callback)
			{
				manageRequest.blog.order.create(order.name,order.index,util.resultCallback(callback));
			},
			//更新
			update:function(order,callback)
			{
				manageRequest.blog.order.update(order.id,order.name,order.index,util.resultCallback(callback));
			}
		},
		recommend:
		{
			//返回自荐
			find:function(pn,callback)
			{
				manageRequest.blog.recommend.find(pn,util.pageCallback(callback));
			},
			//通过
			pass:function(id,callback)
			{
				manageRequest.blog.recommend.pass(id,util.resultCallback(callback));
			},
			//拒绝
			refuse:function(id,callback)
			{
				manageRequest.blog.recommend.refuse(id,util.resultCallback(callback));
			}
		}
	};
	
	manageService.favorite=
	{
		type:
		{
			//返回类型
			find:function(callback)
			{
				manageRequest.favorite.type.find(util.dataCallback(callback));
			},
			//更新
			update:function(type,callback)
			{
				manageRequest.favorite.type.update(type.id,type.name,type.url,util.resultCallback(callback));
			}
		}
	};
	
	manageService.comment=
	{
		//恢复已删除
		recoverDelete:function(id,callback)
		{
			manageRequest.comment.recoverDelete(id,util.resultCallback(callback));
		},
		//删除
		remove:function(id,callback)
		{
			manageRequest.comment.remove(id,util.resultCallback(callback));
		},
		//返回删除
		findDelete:function(pn,callback)
		{
			manageRequest.comment.findDelete(pn,util.pageCallback(callback));
		},
		report:
		{
			//返回
			find:function(pn,callback)
			{
				manageRequest.comment.report.find(pn,util.pageCallback(callback));
			},
			//根据ID返回
			findById:function(id,callback)
			{
				manageRequest.comment.report.findById(id,callback);
			}
		}
	};
	
	manageService.cache=
	{
		node:
		{
			//返回
			find:function(pn,callback)
			{
				manageRequest.cache.node.find(pn,util.pageCallback(callback));
			},
			//更新
			update:function(cacheNode,callback)
			{
				manageRequest.cache.node.update(cacheNode.id,cacheNode.host,cacheNode.port,util.resultCallback(callback));
			},
			//创建
			create:function(cacheNode,callback)
			{
				manageRequest.cache.node.create(cacheNode.host,cacheNode.port,util.resultCallback(callback));
			},
			//删除
			remove:function(id,callback)
			{
				manageRequest.cache.node.remove(id,util.resultCallback(callback));
			}
		}
	};
	
	return manageService;
});